package com.kelvin.paopao.module.peisong.list;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.PeisongListAdapter;
import com.kelvin.paopao.api.responseBean.PeisongModel;
import com.kelvin.paopao.injector.components.DaggerPeisongListComponent;
import com.kelvin.paopao.injector.modules.PeisongListModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.peisong.mypeisong.MyPeisongActivity;
import com.kelvin.paopao.widgets.CustomToolbar;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by MCK on 2017/5/31.
 */

public class PeisonglistActivity extends BaseActivity<IPeisongListPresenter>
        implements IPeisongListView, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private LinearLayoutManager manager;

    @Inject
    PeisongListAdapter adapter;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_peisong_list;
    }

    @Override
    protected void initInjector() {
        DaggerPeisongListComponent.builder().peisongListModule(new PeisongListModule(this))
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        customToolbar.showBackIcon(this);
        customToolbar.inflateMenu(R.menu.menu_peisong);
        customToolbar.setMenuOnClick(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                MyPeisongActivity.launch(PeisonglistActivity.this);
                return true;
            }
        });
        manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                showRequestDialog(((PeisongModel) adapter.getData().get(position)).getoId());
            }
        });
        adapter.setOnLoadMoreListener(this, recyclerView);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mPresenter.getData(isRefresh);
    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, PeisonglistActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void loadData(List<PeisongModel> models) {
        adapter.setNewData(models);
    }

    @Override
    public void loadMoreData(List<PeisongModel> models) {
        adapter.addData(models);
    }

    @Override
    public void showEmpty() {
        View nodataView = LayoutInflater.from(this).inflate(R.layout.view_no_data,
                (ViewGroup) recyclerView.getParent(), false);
        adapter.setEmptyView(nodataView);
    }

    @Override
    public void onLoadMoreRequested() {
        if (adapter.getData().size() < 30) {
            //不再加载更多
            adapter.loadMoreEnd(true);
        } else {
            mPresenter.getMoreData();
        }
    }

    private void showRequestDialog(final String oId) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示").setMessage("是否报名配送？").
                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mPresenter.requestPeisong(oId);
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();
    }
}
